package com.ttpai.boot.lbs.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.List;

public class JsonUtil {

    /**
     * 默认 序列化配置
     */
    private static final SerializerFeature[] SERIALIZER_FEATURES = {
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullNumberAsZero,
            SerializerFeature.WriteNullStringAsEmpty,
            SerializerFeature.WriteNullListAsEmpty,
            SerializerFeature.WriteNullBooleanAsFalse,
            SerializerFeature.DisableCircularReferenceDetect,
            SerializerFeature.BrowserCompatible
    };

    /**
     * 默认 解析配置
     */
    private static final Feature[] FEATURES = {
    };

    public static String toJson(Object object) {
        return toJson(object, SERIALIZER_FEATURES);
    }

    public static String toJson(Object object, SerializerFeature[] features) {
        return JSON.toJSONString(object, features);
    }

    public static final <T> T toObject(String json, Class<T> clazz) {
        return toObject(json, clazz, FEATURES);
    }

    private static final <T> T toObject(String json, Class<T> clazz, Feature[] features) {
        return JSON.parseObject(json, clazz, features);
    }

    public static final <T> List<T> toObjects(String json, Class<T> clazz) {
        return JSON.parseArray(json, clazz);
    }

}

